/*
* detect_face_test.cpp
* client端人脸检测测试
*  Created on: 2018年05月13日
*      Author: guyadong
*/
#include <iostream>
#include <faceapiclient/FaceApiIfClient.h>
#include "BaseCmdParam.h"
#include <gfaux_tools/gfaux_tools.h>
#include "sample_log.h"

using namespace gdface;
using namespace net::gdface::sdk;
using namespace gdface::ui;
using namespace gdface::image;
using namespace net::gdface::sdk;
using namespace codegen;
namespace gdface {
	class DectectConfig : public BaseClientConfig {
	public:
		std::string image;
		bool show;
		DectectConfig() {
			// --jpegimg 指定要检测的图像文件
			cmdparam.add<std::string>("jpegimg", 0, "jpeg jpegimg path", false,"images/he049.jpg");
			// --show 指定是否以在图像上显示检测到人脸位置
			cmdparam.add<bool>("show", 0, "show face rectangle on jpegimg", false, true);
		}
	protected:
		void afterParse() {
			BaseClientConfig::afterParse();
			image = cmdparam.get<std::string>("jpegimg");
			show = cmdparam.get<bool>("show");
		}
	};
}

int main(int argc, char *argv[]) {

	DectectConfig param;
	param.parse(argc, argv);

	auto client = FaceApiIfClient(param.host, param.port);
	try {
		SAMPLE_OUT("connect service:{}@{} ...", param.port, param.host);
		SAMPLE_OUT("IMAGE FILE:{}",param.image);
		std::shared_ptr<std::vector<CodeInfo>> codes;
		//auto imgData = gdface::load_string(param.image.c_str());
		// filename
		auto imgData = get_image_binary_raii(param.image);
		// stream
		//std::ifstream ifs(param.image, std::ios::binary);
		//auto imgData = get_image_binary_raii(ifs);
		//ifs.close();

#if 0
		//FRect rect;
		//rect.__set_left(0);
		//rect.__set_top(0);
		//rect.__set_width(738);
		//rect.__set_height(1024);
		// 测试detectFace原生接口函数
		codes = decorator->detectFace(imgData);
#endif
#if 1
		// 测试detectFace 图像矩阵接口
		auto mat = decompress_jpeg_mem(imgData->c_str(), imgData->size());
		// 调用 detectFaceGeneric()接口方法;
		{
			auto jpegimg = get_image_binary_raii(mat);
			codes = client.detectFace(jpegimg());
		}
#endif
		
		if (param.show) {
			showImage(*codes, *imgData,"detectFace response: ");
		}
		else {
			showText(*codes, "detectFace response: ");
		}
	}
	catch (std::exception& tx) {
		SAMPLE_ERR("ERROR: {}", tx.what());
	}
}